#include <iostream>
#include <sys/shm.h>
#include "TestClass.h"
#include "SharedMemory.h"
using namespace std;

int main()
{
    shared_memory::SharedMemory<TestClass> sharedMemory("/home/sherlock");
    if (!sharedMemory.attach(shared_memory::ReadOnly))
    {
        cout << "attach fail" << endl;
        return -1;
    }
    TestClass *data = sharedMemory.data();
    while (true)
    {
        cout << data->getData().c_str() << endl;
    }

    return 0;
}

//int main()
//{
//    key_t key = ftok("/home", 0);
//    cout << key << endl;
//    int shmid = shmget(key, sizeof(TestClass), 0666 | IPC_CREAT);
//    void *shm = shmat(shmid, (void *) nullptr, 0);
//    TestClass *test = (TestClass *) shm;
//
//    while (true)
//    {
//        int num = test->getNum();
//        cout << num << endl;
//    }
//
//    shmdt(shm);//断开关联
//    shmctl(shmid, IPC_RMID, nullptr);//删除共享内存
//
//    return 0;
//}
